package com.tcm.ucenter.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.lang.DateUtils;
import com.base.core.utils.R;
import com.tcm.ucenter.domain.dto.UserDTO;
import com.tcm.ucenter.domain.enums.Const;
import com.tcm.ucenter.domain.vo.UserVO;
import com.tcm.ucenter.service.UserDetailService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/ucenter")
public class UserController {

    @DubboReference(version = "0.0.1-alpha", check = false, lazy = true)
    private UserDetailService userDetailService;

    /**
     * 更新用户资料
     *
     * @param userDto
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public R update(@RequestBody UserDTO userDto) {
        try {
            userDetailService.update(userDto);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 新增/更新用户资料
     *
     * @param userDto
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    public R save(@RequestBody UserDTO userDto) {
        try {
            userDetailService.save(userDto);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/view")
    public R view(@RequestParam("userId") Long userId) {

        try {
            UserDTO userDTO = userDetailService.findUserByUserId(userId);
            if(ObjectUtil.isEmpty(userDTO)){
                return R.success();
            }
            UserVO userVO = new UserVO();
            BeanUtil.copyProperties(userDTO, userVO);
            if (ObjectUtil.isNotEmpty(userDTO.getAvatar())) {
                userVO.setAvatar(Const.MEDIA_URL + userDTO.getAvatar());
            }
            String birthday = DateUtils.formatDate(userDTO.getYear(), userDTO.getMonth(), userDTO.getDay());
            userVO.setAge(DateUtil.ageOfNow(birthday));
            userVO.setBirthday(birthday);
            return R.success(userVO);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }

    }

    /**
     * 获取用户信息
     *
     * @param token
     * @return
     */
    @GetMapping("/parse/token")
    public R parseToken(@RequestParam("token") String token) {

        try {
            UserDTO userDTO = userDetailService.findUserByToken(token);
            if(ObjectUtil.isEmpty(userDTO)){
                return R.fail("用户信息不存在");
            }
            UserVO userVO = new UserVO();
            BeanUtil.copyProperties(userDTO, userVO);
            if (ObjectUtil.isNotEmpty(userDTO.getAvatar())) {
                userVO.setAvatar(Const.MEDIA_URL + userDTO.getAvatar());
            }
            return R.success(userVO);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }

    }


}
